package com.pannadchen.commonscli;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;

public class CommonsCliTest {
	public static void main(String[] args){
		CommandLineParser parser = new PosixParser();
		Options options = new Options();
		
		// 1. 定义
		options.addOption("h", false, "List for short.");
		options.addOption("n", true, "NDS services list.");
		options.addOption("d", true, "das's ip address.");
		options.addOption("s", true, "sub das's ip address.");
		
		CommandLine cmd = null;
		try {
			// 2. 解析
			cmd = parser.parse(options, args);
			
			// 3. 查询
			if(cmd.hasOption("h")){
				System.out.println("This is Help info.");
				HelpFormatter help = new HelpFormatter();
//				help.printHelp("Test", options);
				help.printHelp("java -jar CommonCliTest.jar", options, true);
			}
			else {				
				String db = cmd.getOptionValue("D");
				String das = cmd.getOptionValue("d");
				String sub_das = cmd.getOptionValue("s");

				System.out.println(db);
				System.out.println(das);
				System.out.println(sub_das);
				
				StructDa da = new StructDa();
				da.setDb(db);
				da.setDas(das);
				da.setSubDas(sub_das);
				System.out.println(da.toString());
			}
			
		} catch (ParseException e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
		
		System.out.println("==== FINISHED! ====");
	}
}
